<?php

/*
本周生产计划
*/

namespace Modules\Pmcsignage\Http\Repositories;

use App\Services\KingDeeService;
use App\Services\UnitService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Modules\Inventory\Repository\InventoryOrderRepository;
use Modules\Purchasing\Models\Provisional;
use Modules\Purchasing\Models\Provisionalschedule;
use Illuminate\Support\Facades\DB;
use Catch\Exceptions\FailedException;
use Modules\Base\Service\BoxService;
use Modules\Purchasing\Models\Supplier;
use Modules\Purchasing\Models\Supplierschedule;
use Modules\Purchasing\Models\Purchaseorder;
use Modules\Purchasing\Models\Purchaseorderschedule;
use Modules\Warehouse\Models\Storagelocation;
use Modules\Purchasing\Models\Inspectionlist;
use Modules\Purchasing\Models\Inspectionlistcheck;
use extend\services\warehouse\{warehouseService};//引入自定义类
use Modules\Intelligent\Http\Repositories\InspectionlistReget;
use Modules\Intelligent\Models\Otherstorage;
use Modules\Intelligent\Models\Otherstorageschedule;
use Modules\Intelligent\Models\Lotbinding;
use Modules\Takestock\Models\InventoryOrderDetailschedule;
use Modules\Inventory\Models\InventoryOrderDetail;
use Modules\Intelligent\Models\Otheroutboundfrom;
use Modules\Intelligent\Models\OtheroutboundfromSchedule;
use App\Enums\InvTransDocType;
use App\Services\InventoryService;
use App\Enums\InventoryOrderStatus;
use Modules\Production\Models\ProductionOrder;
use Modules\Production\Models\ProductionOrderDetail;
use Modules\Pmcsignage\Models\ProductionTheweekplan;


class ProductionTheweekReget
{

    public static function listtable($data = [])
    {
        return self::getBuildertable($data)->paginate($data['limit'] ?? 100);
    }


    public static function getBuildertable(array $data = []): Builder
    {

        $builder = ProductionTheweekplan::query();

        if (isset($data['theweek'])) {
            $first_day_of_week = strtotime('this week Monday');
            $last_day_of_week = strtotime('this week Sunday');
            $str = date('Y-m-d:H:i:s',$first_day_of_week);
            $end = date('Y-m-d:H:i:s',$last_day_of_week);
            $builder->where('fplannedstarttime','>=' , $str);
            $builder->where('fplannedstarttime','<=' , $end);
        }
       
        if (isset($data['fplannedstarttime'])) {
            $builder->where('fplannedstarttime','>=' , $data['fplannedstarttime'][0]);
            $builder->where('fplannedstarttime','<=' , $data['fplannedstarttime'][1]);
        }
       
       
        if (isset($data['created_at'])) {
            $str = strtotime($data['created_at'].' 00:00');
            $end = strtotime($data['created_at'].' 23:59');
            $builder->where('created_at','>=' , $str);
            $builder->where('created_at','<=' , $end);
        }
        
        
        if (isset($data['fbillno'])) {
            $fbillno = ProductionOrder::select('fbillno','id')->where('fbillno', 'like', "%{$data['fbillno']}%")->get()->toArray();
             $fmaterial_fbillno = [];
            foreach($fbillno as $k=>$v){
                $fmaterial_fbillno[] = $v['id'];
            }
            $fmaterial_fbillno = array_unique($fmaterial_fbillno);
            
            $builder->whereIn('production_order_id',$fmaterial_fbillno);
        }
        
         if (isset($data['fmaterialid'])) {
            $fbillno = ProductionOrder::select('fmaterialid','id')->where('fmaterialid', 'like', "%{$data['fmaterialid']}%")->get()->toArray();
             $fmaterial_fbillno = [];
            foreach($fbillno as $k=>$v){
                $fmaterial_fbillno[] = $v['id'];
            }
            $fmaterial_fbillno = array_unique($fmaterial_fbillno);
            
            $builder->whereIn('production_order_id',$fmaterial_fbillno);
        }
        
         if (isset($data['fmaterialname'])) {
            $fbillno = ProductionOrder::select('fmaterialname','id')->where('fmaterialname', 'like', "%{$data['fmaterialname']}%")->get()->toArray();
             $fmaterial_fbillno = [];
            foreach($fbillno as $k=>$v){
                $fmaterial_fbillno[] = $v['id'];
            }
            $fmaterial_fbillno = array_unique($fmaterial_fbillno);
            
            $builder->whereIn('production_order_id',$fmaterial_fbillno);
        }
        
        if (isset($data['fworkshopid_name'])) {
            $fbillno = ProductionOrder::select('fworkshopid_name','id')->where('fworkshopid_name', 'like', "%{$data['fworkshopid_name']}%")->get()->toArray();
             $fmaterial_fbillno = [];
            foreach($fbillno as $k=>$v){
                $fmaterial_fbillno[] = $v['id'];
            }
            $fmaterial_fbillno = array_unique($fmaterial_fbillno);
            
            $builder->whereIn('production_order_id',$fmaterial_fbillno);
        }
        
         if (isset($data['fline'])) {
            $fbillno = ProductionOrder::select('fline','id')->where('fline', 'like', "%{$data['fline']}%")->get()->toArray();
             $fmaterial_fbillno = [];
            foreach($fbillno as $k=>$v){
                $fmaterial_fbillno[] = $v['id'];
            }
            $fmaterial_fbillno = array_unique($fmaterial_fbillno);
            
            $builder->whereIn('production_order_id',$fmaterial_fbillno);
        }
        

        if (isset($data['plantype'])) {
            if(!empty($data['plantype'])){
                 $builder->where('plantype',$data['plantype']);
            }
        }
        
        $builder->orderBy('id', 'desc');
        return $builder;
  
    }

      /*
    数据添加方法
    */
    public static function create(array $data)
    {
        
        $ProductionTheweekplan = new ProductionTheweekplan;
        return self::createOrSave($ProductionTheweekplan, $data);
    }

    //数据添加方法
    public static function createOrSave(ProductionTheweekplan $ProductionTheweekplan,$data)
    {
         
        try {
            //开始起事务
            DB::beginTransaction();            
            foreach($data as $k=>$v){
                $data[$k]['creator_id'] = UnitService::getUserId();
                $data[$k]['created_at'] = time();
            }
            ProductionTheweekplan::insert($data);
            DB::commit();
            return true;
        } catch (\Exception $e) {
            DB::rollBack();
            return $e;
        }
    }


   





}
